﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Service;
using Models;
using Web.Helper;
using Common;

namespace Web
{
    /// <summary>
    /// Summary description for Service
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class Service : System.Web.Services.WebService
    {
        #region public string CallService1(string skey, string model)

        [WebMethod(EnableSession = true)]
        public string CallService1(string skey, string model)
        {
            try
            {
                string result = string.Empty;

                if (skey == Enums.SKey.module.ToString())
                {
                    IModuleManager iModuleManager = ServiceFactory.CreateModuleManager();

                    IList<ModuleInfo> module = iModuleManager.GetModuleInfoByEnity(JSONHelper.JSONToObject<ModuleInfo>(model));

                    result = JSONHelper.ObjectToJSON(module);
                }
                else if (skey == Enums.SKey.item.ToString())
                {
                    IItemManager iItemManager = ServiceFactory.CreateItemManager();

                    IList<ItemInfo> module = iItemManager.GetItemInfoByEnity(JSONHelper.JSONToObject<ItemInfo>(model));

                    result = JSONHelper.ObjectToJSON(module);
                }

                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region public int CallService2(string skey, string model)

        [WebMethod(EnableSession = true)]
        public int CallService2(string skey, string model)
        {
            try
            {
                int result = 0;

                if (skey == Enums.SKey.module.ToString())
                {
                    IModuleManager iModuleManager = ServiceFactory.CreateModuleManager();

                    result = iModuleManager.DoSaveChanges(JSONHelper.JSONToObject<ModuleInfo>(model));
                }
                else if (skey == Enums.SKey.item.ToString())
                {
                    IItemManager iItemManager = ServiceFactory.CreateItemManager();

                    result = iItemManager.DoSaveChanges(JSONHelper.JSONToObject<ItemInfo>(model));
                }

                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion
    }
}
